gusucode.com > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM源码程序 > 支持向量机工具箱 - LIBSVM OSU_SVM LS_SVM\stprtool\pca\ecov.m

    function [V,eigv]=ecov(X)
% ECOV returns non-zero eigenvectors of cov(X).
%  [V,eigv]=ecov(X)
%
% This function uses one-to-one correspondence between
% non-zero eigenvalues and eigenevectors of matrices
% (Xc * Xc') and (Xc' * Xc), where Xc is centered X.
%
% Result of this function is the same as eig(cov(X'))
% but computation is different. The ecov is more
% efficient if data dimension >> number of data.
%
% Result eigenvectors and eigenvalues are return 
% descending order.
%
% Input:
%  X [d x l] d-dimensional patterns set of size l.
%  
% Output:
%  V [d x m] eigenvectors of cov(X).
%  eigv [1 x m] non-zero eigenvalues.
%  

% Statistical Pattern Recognition Toolbox, Vojtech Franc, Vaclav Hlavac
% (c) Czech Technical University Prague, http://cmp.felk.cvut.cz.
% Modifications:
%  21-may-2001, V.Franc, created 

n=size(X,2);
d=size(X,1);

% centering data
if n > 1,
  mi=mean(X')';
  X=X-repmat(mi,1,n);
else
  mi=zeros(d,1);
end

% compute matrix K
K=X'*X/(n-1);

% compute eigenvectors and sort them
[U,D]=eig(K);
[eigv,inx]=sort(-real(diag(D)));
eigv=-eigv;
U=U(:,inx);

% compute eigenvectors of cov(X)
V=[];
for i=1:min(d,n),
  if eigv(i) > 0,
    V=[V,X*U(:,i)/sqrt(eigv(i))];
  end
end

eigv = eigv(1:min(d,n));
V = V/norm(V);

return;